package com.duan.oauth.controller;

import com.duan.entity.Result;
import com.duan.oauth.service.AuthService;
import com.duan.utils.CookieUtil;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 认证中心
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Value("${auth.clientId}")
    private String clientId;

    @Value("${auth.clientSecret}")
    private String clientSecret;

    @Value("${auth.cookieDomain}")
    private String cookieDomain;

    @Value("${auth.cookieMaxAge}")
    private int cookieMaxAge;

    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password, HttpServletResponse response) {
        Map<String,String> login = authService.login(username, password, clientId, clientSecret);
        CookieUtil.addCookie(response,cookieDomain,"/","token",login.get("jti"),cookieMaxAge,false);
        return Result.ok(login);
    }
}
